package main

import (
	"fmt"
	"sync"
)

var dogChan = make(chan struct{}, 1)
var catChan = make(chan struct{}, 1)
var fishChan = make(chan struct{}, 1)

func main() {

	var wg sync.WaitGroup
	wg.Add(12)

	dogChan <- struct{}{}
	for i := 0; i < 4; i++ {
		go writeDog(dogChan, &wg, catChan)
		go writeCat(catChan, &wg, fishChan)
		go writeFish(fishChan, &wg, dogChan)
	}

	wg.Wait()
}

func writeDog(ch chan struct{}, wg *sync.WaitGroup, catChan chan struct{}) {

	<-ch
	fmt.Println("dog")
	wg.Done()
	catChan <- struct{}{}
}

func writeCat(ch chan struct{}, wg *sync.WaitGroup, fishChan chan struct{}) {

	<-ch
	fmt.Println("cat")
	wg.Done()
	fishChan <- struct{}{}
}

func writeFish(ch chan struct{}, wg *sync.WaitGroup, dogChan chan struct{}) {
	<-ch
	fmt.Println("fish")
	wg.Done()
	dogChan <- struct{}{}
}
